# delimit ;
clear ;
set more off ;
version 12.0 ;

cd "replication" ;

* ***************************************************************************** ;
* Carpena, Fenella, and Bilal Zia. "The causal mechanism of financial education: 
* Evidence from mediation analysis." Journal of Economic Behavior & Organization 
* 177 (2020): 143-184.
* 
* This do-file creates Appendix Table A5 of the paper
* ***************************************************************************** ;

* ***************************************************************************** ;
* get endline data
* ***************************************************************************** ;

use "./data/endline.dta", clear ;

* merge with baseline ;
merge 1:1 id using "./data/baseline.dta" ;
tab _merge ;

* correction to the variable label ;
label var savings_bank "has bank savings account" ;

* ***************************************************************************** ;
* sample selection
* ***************************************************************************** ;

* drop households with baseline but no endline ;
drop if _merge == 2 ; 
drop _merge ;

* drop households with no financial knowledge scores ; 
egen temp_knowl = rowmean(numer_fin_return-attit_budget) ;
drop if temp_knowl == . ;
drop temp_knowl ; 

* identify respondents with endline measures of numeracy ;
merge 1:1 id using "./data/financial-knowledge-short-term", keepusing(id) ;
tab _merge ;
keep if _merge == 3 ;
drop _merge ;

* drop households with missing discount rate (control variable) ;
drop if disc_rate == . ;

* ***************************************************************************** ;
* create financial knowledge scores
* ***************************************************************************** ;

egen end_numeracy = rowmean(numer_fin_return-numer_int_rate) ;
egen end_awareness = rowmean(aware_budget-aware_unprod) ;
egen end_attitudes = rowmean(attit_suggest_ins-attit_budget) ;

* ***************************************************************************** ;
* create treatment dummies
* ***************************************************************************** ;

gen flcounsgoal = (fin_ed_treatment == 1 & couns_treatment == 1 & goal_treatment == 1)  ;
gen flcouns = (fin_ed_treatment == 1 & couns_treatment == 1 & goal_treatment == 0) ;
gen flgoal = (fin_ed_treatment == 1 & couns_treatment == 0 & goal_treatment == 1) ;
gen fl = (fin_ed_treatment == 1 & couns_treatment == 0 & goal_treatment == 0) ;
gen control = (fin_ed_treatment == 0 & couns_treatment == 0 & goal_treatment == 0) ;

* ***************************************************************************** ;
* control variables
* ***************************************************************************** ;

local cvars has_hard_time_saving interested_in_financial inconsistent disc_rate riskaverse  ;

* ***************************************************************************** ;
* APPENDIX TABLE A5: regression with interactions 
* ***************************************************************************** ;

est drop _all ;

* regressions, loop over all outcomes and mediators ; 
foreach y of varlist budget_tried savings_bank loan_purpose_bus_educ ins_life { ;

	foreach m in att awa { ;
	
	* fin ed only ;
	if "`y'" == "budget_tried" { ;
		areg `y' i.fl##c.end_`m' `cvars' if fl | control, a(strata) clu(wave_class) ; 
		est sto `y'`m'1 ;
	} ;
	
	* fin ed + goal ;
	if "`y'" == "budget_tried" | "`y'" == "savings_bank" { ;
		areg `y' i.flgoal##c.end_`m' `cvars' if flgoal | control, a(strata) clu(wave_class) ; 
		est sto `y'`m'2 ;
	} ;
		
	* fin ed + couns ;
	areg `y' i.flcouns##c.end_`m' `cvars' if flcouns | control, a(strata) clu(wave_class) ; 
	est sto `y'`m'3 ;
		
	* fin ed + couns + goal ;
	if "`y'" == "budget_tried" | "`y'" == "savings_bank" | "`y'" == "ins_life" { ;
		areg `y' i.flcounsgoal##c.end_`m' `cvars' if flcounsgoal | control, a(strata) clu(wave_class) ;
		est sto `y'`m'4 ;
	} ;
	
} ;
} ;

shell printf "********** Appendix Table A5 ********** \r\n" > "./output/appendix-table-A05.csv" ;

* output results ;
forvalues i = 1/4 { ;	

	esttab *`i'* using "./output/appendix-table-A05.csv", 
		append depvars label
		cells(b(star fmt(%9.3f %9.3f)) se(par)) keep(*#*) drop (0.f*)
		star(* 0.10 ** 0.05 *** 0.01) 
		stats(N, fmt(%9.0f) 
		labels("Observations"))	;
		
	shell printf "\r\n" >> "./output/appendix-table-A05.csv" ;
} ;

exit ;
